﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CarbonExchange.AmeeQuery;
using System.Xml.Linq;

namespace CarbonExchange.Bll
{
    public abstract class AmeeFactoryBase<TItem>
    {
        protected AmeeFactoryBase()
        {
            Command = StructureMap.ObjectFactory.GetInstance<IAmeeCommand>();
        }

        protected AmeeFactoryBase(IAmeeCommand command)
        {
            Command = command;
        }

        protected IAmeeCommand Command { get; private set; }

        protected string BuildAmeeDataPath(string path)
        {
            var tmp = string.Concat("data/", path);
            return StripDoubleSlash(tmp);
        }

        protected string BuildAmeeDataPath(string path, string id)
        {
            var tmp = string.Concat("data/", path, "/", id);
            return StripDoubleSlash(tmp);
        }

        protected string BuildAmeeProfilePath(string profileId, string path)
        {
            var tmp = string.Concat("profiles/", profileId, "/", path);
            return StripDoubleSlash(tmp);
        }

        protected string BuildAmeeProfilePath(string profileId, string path, string id)
        {
            var tmp = string.Concat("profiles/", profileId, "/", path, "/", id);
            return StripDoubleSlash(tmp);
        }

        protected string BuildAmeeDataDrillDownPath(string path, string choices)
        {
            var tmp = string.Concat("data/", path, "/drill?", choices);
            return StripDoubleSlash(tmp);
        }

        private string StripDoubleSlash(string val)
        {
            while (val.IndexOf("//") >= 0)
            {
                val = val.Replace("//", "/");
            }
            return val;
        }

        protected XElement FindProfileItemByName(XDocument doc, string name)
        {
            XElement elem = null;
            if (doc.Root.Element("ProfileCategoryResource") != null
                    && doc.Root.Element("ProfileCategoryResource").Element("Children") != null
                    && doc.Root.Element("ProfileCategoryResource").Element("Children").Element("ProfileItems") != null
                    && doc.Root.Element("ProfileCategoryResource").Element("Children").Element("ProfileItems").Elements("ProfileItem").Count() > 0
                )
            {
                var profileItems = doc.Root.Element("ProfileCategoryResource").Element("Children").Element("ProfileItems").Elements("ProfileItem");
                var actual = from item in profileItems where item.Element("name") != null && item.Element("name").Value == "Actual" select item;
                if (actual != null)
                {
                    elem = actual.FirstOrDefault();
                }
            }
            return elem;
        }

        protected string ConvertToShortAmeeDate(DateTime date)
        {
            return date.ToString("yyyyMMdd");
        }

        protected string ConvertToAmeeDate(DateTime date)
        {
            return date.ToString("yyyy-MM-dd hh:mm:ss.f");
        }

        protected DateTime ConvertToAmeeDate(string date)
        {
            DateTime tmp = DateTime.MinValue;
            if (!string.IsNullOrEmpty(date))
            {
                if (date.Length == 8)
                {
                    tmp = DateTime.Parse(string.Concat(date.Substring(6, 2), "/", date.Substring(4, 2), "/", date.Substring(0, 4)));
                }
                else
                {
                    try
                    {
                        tmp = DateTime.Parse(date);
                    }
                    catch(FormatException)
                    {
                        throw new ApplicationException(string.Format("Date in format '{0}' not handled", date));
                    }

                }
            }
            return tmp;
        }



    }
}
